package com.sljy.hzk.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sljy.hzk.R;
import com.sljy.hzk.entity.ExamMainEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created  on 2018/7/4 0004
 *
 * @author 唐开阔
 * @describe 考试的历史记录
 */
public class ExamPlanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ExamMainEntity.ExamListBean> examListBeans;
    private Context context;
    private final static int TYPE_ITEM = 1;//正常项
    private final static int TYPE_HEAD = 2;//日期头部

    public ExamPlanAdapter(List<ExamMainEntity.ExamListBean> examListBeans, Context context) {
        this.examListBeans = examListBeans;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ITEM:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_exam_plan, parent, false);
                return new ItemViewHolder(view);
            case TYPE_HEAD:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_exam_plan_head, parent, false);
                return new HeadViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE_ITEM:
                ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
                itemViewHolder.bindData(examListBeans.get(position));
                break;
            case TYPE_HEAD:
                HeadViewHolder headViewHolder = (HeadViewHolder) holder;
                headViewHolder.bindData(examListBeans.get(position+1).getDate());
                break;
        }
    }

    /**
     * 判断头部的位置：前提-数据已经按照时间排序
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (examListBeans.get(position).getDate().equals("-1")) {
            return TYPE_HEAD;
        }
        return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return examListBeans == null?0: examListBeans.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_pos)
        TextView tvPos;
        @BindView(R.id.tv_time)
        TextView tvTime;
        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
        public void bindData(ExamMainEntity.ExamListBean data){
            tvName.setText(data.getExamName());
            tvPos.setText(data.getPartition());
            tvTime.setText(data.getDate());
        }
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
        public void bindData(String time){
            tvName.setText(time);

        }
    }
}
